import { Application, Session } from "mydog";
import { FrameClient, FrameSync } from "../../../app/framesync/FrameSync";
import { RoomMgr } from "../../../app/roomMgr";
import { onUserLeave } from "../../connector/handler/main";

export default class Handler {
    private _roomMgr: RoomMgr;

    constructor(app: Application) {
        this._roomMgr = app.get<RoomMgr>("roomMgr");

        //temp code
   
    }

    /**
     * 聊天
     */
    chat(msg: { "msg": string }, session: Session, next: Function) {
        let room = this._roomMgr.getRoom(session.get("roomId"));

        if(msg.msg == "framesync"){
            room.frameSync.start();//开始帧同步测试代码
        }
        room.chat({ "uid": session.uid, "msg": msg.msg });
    }

    framesync(msg: FrameClient, session: Session, next: Function) {
        //temp code
        var room = this._roomMgr.getRoom(session.get("roomId"));
        if(!room){
            console.log("error:房间销毁了，客户端还发帧同步数据上来 roomId:" + session.get("roomId"));
            return;
        }
        room.frameSync.input(msg);
    }

    leaveRoom(msg: any, session: Session, next: Function){
        onUserLeave(session);
    }
}
